package com.bdsys.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bdsys.common.action.BasicAction;
import com.bdsys.common.constants.Constants;
import com.bdsys.form.Message;
import com.bdsys.form.User;
import com.bdsys.service.MessageService;
import com.bdsys.service.UserService;
import com.bdsys.servicedto.MessageDetailDTO;
import com.bdsys.servicedto.MessageServiceDTO;
import com.bdsys.servicedto.UserDetailDTO;
import com.bdsys.servicedto.UserServiceDTO;
import com.bdsys.vo.UserVo;

@SuppressWarnings("serial")
public class MessageAction extends BasicAction implements Constants {

	Message message;

	private MessageService messageService = (MessageService) getService(MESSAGE_SERVICE);

	public String addMessage() {
		MessageService Service = (MessageService) getService(USER_SERVICE);
		MessageServiceDTO inServiceDTO = new MessageServiceDTO();
		inServiceDTO.setMessageDetailDTO(this.fromFormToMessageDetailDto());
		userService.addUser(inServiceDTO);
		return EDITSUCCESS;
	}

	public String deleteUserType() {
		UserService userService = (UserService) getService(USER_SERVICE);
		userService.deleteUserType(message.getType());
		return "deletUserType";
	}

	public String checkLoginUser() {
		UserService userService = (UserService) getService(USER_SERVICE);
		String type = userService.checkLoginUser(message.getName(),
				message.getPassword());
		if (type == null) {
			getRequest().put("nouser", "nouser");
			return "loginFail";
		} else {
			return "loginSuccess";
		}

	}

	public String addUserType() {
		UserService userService = (UserService) getService(USER_SERVICE);
		userService.addUserType(Arrays.asList(message.getUserTypes()));
		return "editSuccess";
	}

	public String selectAllUserTypes() {
		UserService userService = (UserService) getService(USER_SERVICE);
		Map<String, String> userTypes = userService.selectAllUserTypes();
		getRequest().put("userTypes", userTypes);
		if (message != null && "userTypeList".equals(message.getAction())) {
			setPage("/user/userTypeList.jsp");
		} else {
			setPage("/user/editUser.jsp");
		}

		return "selectAllUseTypes";
	}

	private MessageDetailDTO fromFormToMessageDetailDto() {
		message = (message == null ? new Message() : message);
		MessageDetailDTO detailDTO = new MessageDetailDTO();
		detailDTO.setContent(message.getContent());
		detailDTO.setId(message.getId());
		detailDTO.setIsRead(message.getIsRead());
		detailDTO.setIsRecieverDelete(message.getIsRecieverDelete());
		detailDTO.setIsSenderDelete(message.getIsSenderDelete());
		detailDTO.setRecieverId(message.getRecieverId());
		detailDTO.setSenderId(message.getSenderId());
		detailDTO.setSendTime(message.getSendTime());
		detailDTO.setTitle(message.getTitle());
		return detailDTO;
	}

	public String selectByConditions() {
		MessageServiceDTO inServiceDTO = new MessageServiceDTO();
		inServiceDTO.setMessageDetailDTO(this.fromFormToMessageDetailDto());
		MessageServiceDTO outServiceDTO = messageService.
				.selectMessageByConditions(inServiceDTO);
		List<UserDetailDTO> list = outServiceDTO.getList();

		List<UserVo> usersVo = new ArrayList<UserVo>();

		for (UserDetailDTO detailDTO : list) {
			usersVo.add(this.fromDetailDTOToVo(detailDTO));
		}

		if (message != null) {
			if ("forUpdate".equals(message.getAction())) {
				getRequest().put("userVo", usersVo.get(0));
			} else {
				getRequest().put("userList", usersVo);
			}
		} else {
			getRequest().put("userList", usersVo);
		}

		return SELECTBYCONDITIONS;

	}

	public Message getMassage() {
		return message;
	}

	private UserVo fromDetailDTOToVo(UserDetailDTO detailDTO) {
		UserVo vo = new UserVo();
		vo.setId(detailDTO.getId());
		vo.setName(detailDTO.getName());
		vo.setType(detailDTO.getType());
		vo.setPhone(detailDTO.getPhone());
		vo.setEmail(detailDTO.getEmail());
		vo.setPassword(detailDTO.getPassword());
		vo.setUclass(detailDTO.getuClass());
		vo.setTypeName(detailDTO.getTypeName());
		return vo;
	}

	public String deleteMessage() {
		MessageService messageService = (MessageService) getService(MESSAGE_SERVICE);
		messageService.deleteMessage(message.getId());
		return EDITSUCCESS;
	}

	public void setUser(Message message) {
		this.message = message;
	}

}
